import { create } from 'zustand'

interface User {
  id: string
  name: string
  email: string
  phone?: string
  avatar?: string
  children: Child[]
  isLoggedIn: boolean
}

interface Child {
  id: string
  name: string
  age: number
  avatar?: string
  interests: string[]
}

interface Book {
  id: string
  title: string
  author: string
  cover: string
  description: string
  ageRange: [number, number]
  category: string
  tags: string[]
  rating: number
  price: number
  isFree: boolean
  pages: number
}

interface Activity {
  id: string
  title: string
  description: string
  image: string
  date: string
  location: string
  price: number
  maxParticipants: number
  currentParticipants: number
  category: string
}

interface AppState {
  // 用户状态
  user: User | null
  currentChild: Child | null
  
  // 内容状态
  books: Book[]
  activities: Activity[]
  featuredBooks: Book[]
  newBooks: Book[]
  
  // UI状态
  isLoading: boolean
  searchQuery: string
  selectedCategory: string
  selectedAgeRange: [number, number]
  
  // 方法
  setUser: (user: User | null) => void
  setCurrentChild: (child: Child | null) => void
  setBooks: (books: Book[]) => void
  setActivities: (activities: Activity[]) => void
  setFeaturedBooks: (books: Book[]) => void
  setNewBooks: (books: Book[]) => void
  setLoading: (loading: boolean) => void
  setSearchQuery: (query: string) => void
  setSelectedCategory: (category: string) => void
  setSelectedAgeRange: (range: [number, number]) => void
}

export const useStore = create<AppState>((set) => ({
  // 初始状态
  user: null,
  currentChild: null,
  books: [],
  activities: [],
  featuredBooks: [],
  newBooks: [],
  isLoading: false,
  searchQuery: '',
  selectedCategory: 'all',
  selectedAgeRange: [0, 12],
  
  // 方法实现
  setUser: (user) => set({ user }),
  setCurrentChild: (currentChild) => set({ currentChild }),
  setBooks: (books) => set({ books }),
  setActivities: (activities) => set({ activities }),
  setFeaturedBooks: (featuredBooks) => set({ featuredBooks }),
  setNewBooks: (newBooks) => set({ newBooks }),
  setLoading: (isLoading) => set({ isLoading }),
  setSearchQuery: (searchQuery) => set({ searchQuery }),
  setSelectedCategory: (selectedCategory) => set({ selectedCategory }),
  setSelectedAgeRange: (selectedAgeRange) => set({ selectedAgeRange }),
}))